package admin.action;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import home.model.Goods;
import home.model.PageBean;
import home.service.GoodsService;

import com.opensymphony.xwork2.ActionSupport;

public class GoodsMngAction extends ActionSupport{
	private GoodsService goodss;
	private List<Goods> goodsList;
	private Goods goodsEdit;
	private int id;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Goods getGoodsEdit() {
		return goodsEdit;
	}
	public void setGoodsEdit(Goods goodsEdit) {
		this.goodsEdit = goodsEdit;
	}
	public List<Goods> getGoodsList() {
		return goodsList;
	}
	public void setGoodsList(List<Goods> goodsList) {
		this.goodsList = goodsList;
	}
	public GoodsService getGoodss() {
		return goodss;
	}
	public void setGoodss(GoodsService goodss) {
		this.goodss = goodss;
	}
	private int page;
	private PageBean pageBean;
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	private String like;
	public String getLike() {
		return like;
	}
	public void setLike(String like) {
		this.like = like;
	}
	public String execute() throws Exception{
		pageBean = goodss.queryForPage(5, page, like);
		goodsList = pageBean.getList();
		System.out.println(goodsList);
		return SUCCESS;
	}
	public String add(){
		return SUCCESS;
	}
	public String addSubmit(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date(System.currentTimeMillis());
		goodsEdit.setCreated_at(df.format(date));
		goodsEdit.setCreated_by("MsgMngAction/addSubmit");
		goodsEdit.setUpdated_at(df.format(date));
		goodsEdit.setUpdated_by("MsgMngAction/addSubmit");

		goodss.saveGoods(goodsEdit);
		return SUCCESS;
	}
	public String edit(){
		goodsEdit = goodss.getGoods(id);
		if(goodsEdit == null) return INPUT;
		return SUCCESS;
	}
	public String editSubmit(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date(System.currentTimeMillis());
		goodsEdit.setUpdated_at(df.format(date));
		goodsEdit.setUpdated_by("MsgMngAction/editSubmit");

		goodss.saveGoods(goodsEdit);
		return SUCCESS;
	}
	public String delete(){
		goodss.deleteGoodsById(id);
		return SUCCESS;
	}
}
